package com.vben.system.biz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vben.common.core.util.R;
import com.vben.system.api.dto.rbac.*;
import com.vben.system.api.entity.SysUser;
import com.vben.system.api.vo.user.SysUserVO;
import com.vben.system.biz.service.SysRbacService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * RBAC 相关接口
 */
@RestController
@RequestMapping("/rbac")
public class SysRbacController {

    @Resource
    private SysRbacService sysRbacService;

    /**
     * 根据角色ID获取菜单ID集合
     */
    @PostMapping("/roleMenuIds")
    public R<List<Long>> roleMenuIds(@RequestBody RoleIdDTO dto) {
        List<Long> menuIds = sysRbacService.getRoleMenuIds(dto.getRoleId());
        return R.ok(menuIds);
    }

    /**
     * 保存角色菜单关系
     */
    @PostMapping("/saveRoleMenu")
    public R<Boolean> saveRoleMenu(@RequestBody SaveRoleMenuDTO dto) {
        Boolean result = sysRbacService.saveRoleMenu(dto.getRoleId(), dto.getMenuIds());
        return R.ok(result);
    }

    /**
     * 通过角色ID获取用户列表
     */
    @PostMapping("/userListByRoleId")
    public R<Page<SysUser>> userListByRoleId(@RequestBody RoleIdDTO dto) {
        return R.ok(sysRbacService.getUserListByRoleId(dto));
    }

    /**
     * 删除用户角色关系
     */
    @PostMapping("/removeUserRole")
    public R<Boolean> removeUserRole(@RequestBody List<RemoveUserRoleDTO> dto) {
        Boolean result = sysRbacService.removeUserRole(dto);
        return R.ok(result);
    }

    /**
     * 获取用户列表-排除指定角色
     */
    @PostMapping("/userListExcludeRoleId")
    public R<Page<SysUserVO>> userListExcludeRoleId(@RequestBody RoleIdDTO dto) {
        Page<SysUserVO> userList = sysRbacService.getUserListExcludeRoleId(dto);
        return R.ok(userList);
    }

    /**
     * 添加用户角色关系
     */
    @PostMapping("/saveUserRole")
    public R<Boolean> saveUserRole(@RequestBody List<SaveUserRoleDTO> dtoList) {
        Boolean result = sysRbacService.saveUserRole(dtoList);
        return R.ok(result);
    }
}